package org.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.example.pojo.RoleMenu;
import org.example.service.IRoleMenuService;
import org.example.vo.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author peak
 * @since 2024-12-23
 */
@RestController
@RequestMapping("/rolemenu")
public class RoleMenuController {
    @Autowired
    IRoleMenuService roleMenuService;
    @PostMapping("/add/{roledId}")
    public ResultBean addRoleMenus(@PathVariable("roledId") Long roleId,
                                   @RequestBody List<Long> menuIds) {
        //首先先进行删除
        boolean remove = roleMenuService.remove(new QueryWrapper<RoleMenu>().eq("role_id", roleId));
        List<RoleMenu> list=new ArrayList<>();
        menuIds.forEach(menuId->{
            list.add(new RoleMenu(null,roleId,menuId));
        });
        //之后在进行批量添加
        boolean saved = roleMenuService.saveBatch(list);
        return saved? ResultBean.SUCCESS("添加角色菜单成功"):ResultBean.ERROR("添加角色菜单失败");
    }

}
